<template>
  <div class="user-group-container">
    <div>
      <el-col :span="8">
        <el-input placeholder="搜索" v-model="params.name" @keyup.enter.native="searchClick">
          <el-button slot="append" icon="el-icon-search" @click="searchClick"></el-button>
        </el-input>
      </el-col>
      <el-col :span="16" style="text-align: right">
        <el-button type="primary" @click="handleAddGroupBtn">添加用户组</el-button>
      </el-col>
    </div>
    <el-table
      :data="groupList"
      border
      style="width: 100%">
      <el-table-column
        prop="id"
        label="id">
      </el-table-column>
      <el-table-column
        prop="name"
        label="用组">
      </el-table-column>
      <el-table-column
        label="操作">
        <template slot-scope="scope">
          <el-button type="warning" icon="el-icon-edit" circle @click="handleModifyGroupClick(scope.row)"></el-button>
          <el-button type="danger" icon="el-icon-delete" circle @click="handleDeleteGroupClick(scope.row.id)"></el-button>
        </template>
      </el-table-column>
    </el-table>
    <center>
      <el-pagination
        background
        layout="total, prev, pager, next, jumper"
        @current-change="handleCurrentChange"
        :total="totalNum">
      </el-pagination>
    </center>

    <el-dialog title="添加用户组" :visible.sync="addGroupFormVisible">
      <el-form  :model="addGroupForm" :rules="addGroupFormRule" ref="addGroupForm" >
        <el-form-item label="用户组" :label-width="addGroupFormLabelWidth" prop="name">
          <el-input v-model="addGroupForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="addGroupFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleAddGroupSubmit" >确 定</el-button>
      </div>
    </el-dialog>

    <el-dialog title="修改用户组" :visible.sync="groupModifyFormVisible">
      <el-form  :model="groupModifyForm" :rules="addGroupFormRule" ref="groupModifyForm" >
        <el-form-item label="用户组" :label-width="addGroupFormLabelWidth" prop="name">
          <el-input v-model="groupModifyForm.name" auto-complete="off"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="groupModifyFormVisible = false">取 消</el-button>
        <el-button type="primary" @click="handleGroupModifySubmit" >确 定</el-button>
      </div>
    </el-dialog>

  </div>
</template>

<script>
  import { getGroupList, createGroup, updateGroup, deleteGroup } from '@/api/groups'
  export default {
    data() {
      return {
        groupList: [],
        totalNum: 0,
        params: {
          page: 1,
          name: ''
        },
        addGroupFormVisible: false,
        addGroupFormLabelWidth: '120px',
        addGroupForm: {
          name: ''
        },
        addGroupFormRule: {
          name: [
            { required: true, message: '请输入活动名称', trigger: 'blur' },
            { min: 3, max: 30, message: '长度在 3 到 30 个字符', trigger: 'blur' }
          ]
        },
        groupModifyFormVisible: false,
        groupModifyForm: {
          id: 0,
          name: ''
        }
      }
    },
    created() {
      this.fetchData()
    },
    methods: {
      fetchData() {
        getGroupList(this.params).then(res => {
          this.groupList = res.results
          this.totalNum = res.count
        })
      },
      handleCurrentChange(val) {
        this.params.page = val
        this.fetchData()
      },
      searchClick() {
        this.params.page = 1
        this.fetchData()
      },
      handleAddGroupBtn() {
        this.addGroupFormVisible = true
      },
      handleAddGroupSubmit() {
        createGroup(this.addGroupForm).then(res => {
          this.$message({
            message: '创建用户组成功',
            type: 'success'
          })
          this.addGroupFormVisible = false
          this.$refs['addGroupForm'].resetFields()
          this.fetchData()
        })
      },
      handleModifyGroupClick(obj) {
        this.groupModifyForm = obj
        this.groupModifyFormVisible = true
      },
      handleGroupModifySubmit() {
        updateGroup(this.groupModifyForm.id, this.groupModifyForm).then(res => {
          this.$message({
            message: '修改用户组成功',
            type: 'success'
          })
          this.groupModifyFormVisible = false
          this.$refs['groupModifyForm'].resetFields()
          this.fetchData()
        })
      },
      handleDeleteGroupClick(id) {
        this.$confirm('是否确认删除用户组?', '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          deleteGroup(id).then(() => {
            this.$message({
              message: '删除成功',
              type: 'success'
            })
            this.fetchData()
          })
        }).catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
      }
    }
  }

</script>
